#include "stdafx.h"
#include "RAM.h"

RAM::RAM(UInt32 memorySize)
{
	finalAddress = memorySize - 1;

	data = gcnew array<Byte>(memorySize);
}

UInt32 RAM::GetValueAt(UInt32 i)
{
	if (i >= 0 && i <= finalAddress - 3)
	{
		return ((UInt32)data[i] << 24) | ((UInt32)data[i + 1] << 16) | ((UInt32)data[i + 2] << 8) | (UInt32)data[i + 3];
	}

	throw gcnew IndexOutOfRangeException(
		"Memory access out of range. Memory start address: 0" + 
		" - Memory end address: " + 
		finalAddress + " - Access Adress: " + i);
}

void RAM::SetValueAt(UInt32 i, UInt32 value)
{
	if (i >= 0 && i <= finalAddress)
	{
		data[i] = value >> 24;
		data[i + 1] = (value << 8) >> 24;
		data[i + 2] = (value << 16) >> 24;
		data[i + 3] = (value << 24) >> 24;

		return;
	}

	throw gcnew IndexOutOfRangeException(
		"Memory access out of range. Memory start address: 0" + 
		" - Memory end address: " + 
		finalAddress + " - Access Adress: " + i);
}

Byte RAM::GetByteAt(UInt32 i)
{
	return data[i];
}

void RAM::SetByteAt(UInt32 i, Byte value)
{
	data[i] = value;
}

Boolean RAM::LoadIntoMemory(array<UInt32>^ values, UInt32 startAddress)
{
	if (values->Length > data->Length - startAddress)
	{
		return false;
	}

	values->CopyTo(data, (int)startAddress);

	return true;
}
